package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.DefaultResponse
import com.gexiaobao.pigeon.app.model.bean.OrderDrippingDetailResponse
import com.gexiaobao.pigeon.app.model.bean.OrderDrippingListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.SettingUtil
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import okhttp3.RequestBody
import java.io.File

/**
 * @Author      : hxw
 * @Date        : 2024/6/22 14:10
 * @Describe    :
 */
class DrippingViewModel : BaseViewModel() {

    private var pageNo: Int = 1
    private val pageSize: Int = 20

    /** 获取订单列表（用户角色）  */
    val drippingOrderResult: MutableLiveData<ListDataUiState<OrderDrippingListResponse.ListBean>> = MutableLiveData()
    fun getOrderDidiListByUserId(isRefresh: Boolean, status: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getOrderDidiListByUserId(pageNo, pageSize, status) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                listData = it.list
            )
            drippingOrderResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, isRefresh = isRefresh, errMessage = it.msg, listData = arrayListOf<OrderDrippingListResponse.ListBean>()
            )
            drippingOrderResult.value = listDataUiState
        })
    }

    /** 获取订单详情 */
    val drippingOrderDetailResult: MutableLiveData<OrderDrippingDetailResponse> = MutableLiveData()
    fun getOrderDidiDetail(id: String) {
        request({ apiService.getOrderDidiDetail(id) }, {
            drippingOrderDetailResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /**  删除/撤销订单 */
    val deleteDrippingOrderResult: MutableLiveData<Any> = MutableLiveData()
    fun deleteOrderDidi(id: String) {
        request({ apiService.deleteOrderDidi(id) }, {
            deleteDrippingOrderResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /**  申请成为接鸽员（用户角色） */
    val applyPigeonCatcherResult: MutableLiveData<Any> = MutableLiveData()
    fun addTransporterApply(body: RequestBody) {
        request({ apiService.addTransporterApply(body) }, {
            applyPigeonCatcherResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 接鸽员申请中关于个人的信息创建（用户角色） */
    val addTransporterResult: MutableLiveData<DefaultResponse> = MutableLiveData()
    fun addTransporterApplyBase(map: Map<String, Any?>) {

        val cardFrontFile = map["identityCardObverse"] as File
        val cardBackFile = map["identityCardReverse"] as File
        val drivingCardFile = map["drivingCard"] as File

        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()
        hashMap["name"] = map["name"].toString()
        hashMap["mobile"] = map["mobile"].toString()
        hashMap["lon"] = map["lon"].toString()
        hashMap["lat"] = map["lat"].toString()
        hashMap["provinceCity"] = map["provinceCity"].toString()
        hashMap["address"] = map["address"].toString()
        hashMap["identityCard"] = map["identityCard"].toString()

        fileList["identityCardObverse"] = cardFrontFile
        fileList["identityCardReverse"] = cardBackFile
        fileList["drivingCard"] = drivingCardFile

        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.addTransporterApplyBase(body) }, {
            addTransporterResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 接鸽员申请中关于汽车的信息创建（用户角色） */
    val addCarResult: MutableLiveData<DefaultResponse> = MutableLiveData()
    fun addTransporterApplyCar(map: Map<String, Any?>) {
        val carFile = map["licensePlate"] as File
        val drivingFile = map["travelCard"] as File

        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()
        hashMap["licensePlateNumber"] = map["licensePlateNumber"].toString()

        fileList["licensePlate"] = carFile
        fileList["travelCard"] = drivingFile

        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.addTransporterApplyCar(body) }, {
            addCarResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }
}